const router = require('express').Router()
const userObj = [
]
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjpbeyJ0b29sdHQiOiJodHRwczovL3Rvb2x0dC5jb20ifV0sImlhdCI6MTY1MzU1NzAyOCwiZXhwIjoxNzc3OTk2Nzk5LCJhdWQiOiJkZGRkZGRkZGQiLCJpc3MiOiJkc2RkZHZkIiwic3ViIjoiZGRkZGRkZGRkZCJ9.LSLB7MCwNcZaQMDPEO5p_gh0U9Cqt7vwHjnP31ww0bc'


const userSuccess = {
  code: '401',
  msg: '用户已存在!'
}
const success = {
  code: '200',
  msg: '注册成功！',
  token

}
// 注册
router.get('/reg/:uname&:upwd', (req, res) => {
  let user = req.params
  // console.log(user);
  const result = userObj.some(e => {
    return e.uname == user.uname && e.upwd == user.upwd
  })
  if (result) {
    res.send(userSuccess)
  } else {
    if (user.uname !== '') {
      userObj.push(user)
      res.send(success)
    }
  }
})

// 登录
router.get('/login', (req, res) => {
  let obj = req.query;
  let uname = obj.uname;
  let upwd = obj.upwd
  let bool = userObj.some(elem => {
    return uname == elem.uname && upwd == elem.upwd
  })
  if (bool) {
    res.send({ code: 200, msg: '登录成功',token })
  } else {
    res.send({ code: 400, msg: '用户名或密码错误' })
  }
})
module.exports = router